package com.qumitech.visualnovel.service.match.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.qumitech.visualnovel.component.util.HttpInvokeUtil;
import com.qumitech.visualnovel.model.common.BaseRsp;
import com.qumitech.visualnovel.model.common.ResultEnum;
import com.qumitech.visualnovel.model.common.BaseResponse;
import com.qumitech.visualnovel.model.common.UrlConstant;
import com.qumitech.visualnovel.pubdef.dto.match.MatchCfgNewDTO;
import com.qumitech.visualnovel.pubdef.dto.match.MatchCfgUpdateDTO;
import com.qumitech.visualnovel.pubdef.dto.match.MatchTerminateDTO;
import com.qumitech.visualnovel.pubdef.dto.match.UserMatchResultBatchNewDTO;
import com.qumitech.visualnovel.model.dao.console.MatchTemplateConfDO;
import com.qumitech.visualnovel.datasource.console.MatchTemplateConfDOMapper;
import com.qumitech.visualnovel.service.match.MatchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.List;
import java.util.ArrayList;

@Service
public class MatchServiceImpl implements MatchService {
    private static final String MATCH_URL = "/admin/match";

    @Autowired
    private MatchTemplateConfDOMapper matchTemplateConfDOMapper;

    @Override
    public BaseRsp listMatchCfg(Map<String, Object> reqMap) {
        String url = UrlConstant.SERVER_URL + MATCH_URL + "/cfg";
        String body = HttpInvokeUtil.httpGet(url, reqMap);
        return JSONObject.parseObject(body, BaseRsp.class);
    }

    @Override
    public BaseRsp addMatchCfg(MatchCfgNewDTO newDTO) {
        String url = UrlConstant.SERVER_URL + MATCH_URL + "/cfg";
        String body = HttpInvokeUtil.httpPostJson(url, JSONObject.toJSONString(newDTO), UrlConstant.DefaultEncode, null);
        return JSONObject.parseObject(body, BaseRsp.class);
    }

    @Override
    public BaseRsp updateMatchCfg(String matchId, MatchCfgUpdateDTO updateDTO) {
        String url = UrlConstant.SERVER_URL + MATCH_URL + "/" + matchId + "/cfg";
        String body = HttpInvokeUtil.httpPutJson(url, JSONObject.toJSONString(updateDTO), UrlConstant.DefaultEncode, null);
        return JSONObject.parseObject(body, BaseRsp.class);
    }

    @Override
    public BaseRsp terminateMatch(String matchId, MatchTerminateDTO terminateDTO) {
        String url = UrlConstant.SERVER_URL + MATCH_URL + "/" + matchId + "/termination";
        String body = HttpInvokeUtil.httpPostJson(url, JSONObject.toJSONString(terminateDTO), UrlConstant.DefaultEncode, null);
        return JSONObject.parseObject(body, BaseRsp.class);
    }

    @Override
    public BaseRsp addMatchResult(String matchId, UserMatchResultBatchNewDTO newDTO) {
        String url = UrlConstant.SERVER_URL + MATCH_URL + "/" + matchId + "/result";
        String body = HttpInvokeUtil.httpPostJson(url, JSONObject.toJSONString(newDTO), UrlConstant.DefaultEncode, null);
        return JSONObject.parseObject(body, BaseRsp.class);
    }

    @Override
    public BaseRsp confirmMatchResult(String matchId) {
        String url = UrlConstant.SERVER_URL + MATCH_URL + "/" + matchId + "/result/confirm";
        String body = HttpInvokeUtil.httpPostJson(url, new JSONObject(), UrlConstant.DefaultEncode, null);
        return JSONObject.parseObject(body, BaseRsp.class);
    }

    @Override
    public BaseRsp listUserMatchResult(String matchId) {
        String url = UrlConstant.SERVER_URL + MATCH_URL + "/" + matchId + "/result";
        String body = HttpInvokeUtil.httpGet(url, new HashMap<>());
        return JSONObject.parseObject(body, BaseRsp.class);
    }

    @Override
    public BaseResponse listMatchTemplate(Long templateId, String gameId, String matchName, String templateName) {
        List<JSONObject> result = new ArrayList<JSONObject>();
        int count = matchTemplateConfDOMapper.getCount(templateId, gameId, matchName, templateName);
        List<MatchTemplateConfDO> items = matchTemplateConfDOMapper.getList(templateId, gameId, matchName, templateName);
        for (MatchTemplateConfDO item : items) {
            JSONObject elemt = new JSONObject();
            elemt.put("template_id", item.getId());
            elemt.put("game_id", item.getGameId());
            elemt.put("match_name", item.getMatchName());
            elemt.put("template_name", item.getTemplateName());
            elemt.put("template_info", JSONObject.parseObject(item.getTemplateInfo()));
            result.add(elemt);
        }
        return new BaseResponse(result, count);
    }

    @Override
    public BaseResponse updateMatchTemplate(JSONObject templateInfo) {
        Long templateId = templateInfo.getLong("template_id");
        String templateName = templateInfo.getString("template_name");
        MatchTemplateConfDO existRecord = matchTemplateConfDOMapper.getMatchTemplateByName(templateName);
        if (existRecord!=null && templateId!=existRecord.getId()) {
            return new BaseResponse(-1, "模板名称已存在");
        }
        MatchTemplateConfDO record = new MatchTemplateConfDO();
        record.setId(templateId);
        record.setGameId(templateInfo.getString("game_id"));
        record.setMatchName(templateInfo.getString("match_name"));
        record.setTemplateName(templateInfo.getString("template_name"));
        record.setTemplateInfo(JSONObject.toJSONString(templateInfo));
        matchTemplateConfDOMapper.updateByPrimaryKeySelective(record);
        return new BaseResponse(0, "success");
    }

    @Override
    public BaseResponse createMatchTemplate(JSONObject templateInfo) {
        String templateName = templateInfo.getString("template_name");
        MatchTemplateConfDO existRecord = matchTemplateConfDOMapper.getMatchTemplateByName(templateName);
        if (existRecord != null) {
            return new BaseResponse(-1, "模板名称已存在");
        }
        MatchTemplateConfDO record = new MatchTemplateConfDO();
        record.setGameId(templateInfo.getString("game_id"));
        record.setMatchName(templateInfo.getString("match_name"));
        record.setTemplateName(templateInfo.getString("template_name"));
        record.setTemplateInfo(JSONObject.toJSONString(templateInfo));
        matchTemplateConfDOMapper.insertSelective(record);
        return new BaseResponse(0, "success");
    }

    @Override
    public BaseResponse deleteMatchTemplate(Long templateId) {
        matchTemplateConfDOMapper.deleteByPrimaryKey(templateId);
        return new BaseResponse(0, "success");
    }

    @Override
    public BaseResponse listMatchTemplateOptions() {
        List<JSONObject> result = new ArrayList<JSONObject>();
        List<MatchTemplateConfDO> items = matchTemplateConfDOMapper.getOptions();
        for (MatchTemplateConfDO item : items) {
            JSONObject elemt = new JSONObject();
            elemt.put("template_id", item.getId());
            elemt.put("template_name", item.getTemplateName());
            result.add(elemt);
        }
        return new BaseResponse(result);
    }

    @Override
    public BaseRsp listMatchValue(String gameId) {
        Map<String, Object> reqMap = new HashMap<>();
        reqMap.put("game_id", gameId);
        String url = UrlConstant.SERVER_URL + MATCH_URL + "/value";
        String body = HttpInvokeUtil.httpGet(url, reqMap);
        return JSONObject.parseObject(body, BaseRsp.class);
    }
}
